// pages/video/video.js
var app = getApp();
var api = require('../../utils/api.js');
var regeneratorRuntime = require("../../utils/regenerator-runtime/runtime-module.js");
Page({

  /**
   * 页面的初始数据
   */
  // 设置分享到朋友圈的标题
  onShareTimeline:function(){
    return {
      title:"智克艺术 - 视频广场",
      query:"",
    }
  },
  data: {
    nolaud:true,//点赞状态
    likes:666,//点赞数量
    foucs:false,//关注
    videoList:[],//视频信息
    check:[],
    preTime:0,
    show:false,
    previewSrc:'',
    col1:[],
    col2:[]
  },
  //获取轮播图
  async getBanner() {
    const that = this
    wx.request({
      url: app.globalData.host+'Video/banner',
      method:'POST',
      success(res){
        console.log(res)
        that.setData({
          bannerList: res.data.data,
        })
      },
      fail(err){
        console.log(err)
      }
    })

  },
  // 关闭遮罩层
  onClickHide(){
    this.setData({
      show:false
    })
  },
  // 预览视频开始
  preVideo(e){
    let url = e.currentTarget.dataset.url
    console.log(e)
    this.setData({
      show:true,
      previewSrc:url
    })
  },
  // 预览视频结束
  endPre(){
    this.setData({
      show:false
    })
  },
  // 记录当前视频播放进度
  preTup(e){
    // console.log('当前时间'+e.detail.currentTime)
    // console.log('总时间'+e.detail.duration)
  },
  // 跳转到视频详情
  goDetail(e){
    // let url = e.currentTarget.dataset.url
    // wx.navigateTo({
    //   url: url,
    // })
    wx.navigateTo({
      url: '/pages/scroll_video/scroll_video',
    })
  },
  toVideo(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/videoDetail/videoDetail?file_id='+id
    })
  },
  getView(){
    for(var index in this.data.videoList){
      let view = this.data.videoList[index].view
      this.setData({
        view:videoList[index].view==null?0:videoList[index].view
      })
    }
  },
  getVideoList(page,search){
    let params={
      token: wx.getStorageSync('token'),
      page,
      search
    }
    const that = this
    wx.request({
      url: app.globalData.host+'Video/index',
      method:'GET',
      // header:{"Accept": "*/*"},
      data:params,
      success(res){
        let videoList = that.data.videoList
        let videoList1 = res.data.data
        if (page == 1) {
          videoList = res.data.data;
        } else {
          videoList = [...videoList, ...videoList1]
        }
        let col1 = that.data.col1
        let col2 = that.data.col2
        for(var index in videoList){
          index % 2 == 0?col1.push(videoList[index]):col2.push(videoList[index])
        }
        that.setData({
          col1,
          col2,
          page,
          pageNum:videoList1.length,
        })
      },
      fail(err){
        wx.showToast({
          title: res.data.msg,
        })
      }
    })
  },
  // 更新点赞状态
  updateLaud(file_id){
    const that = this
    let token = wx.getStorageSync('token')
    wx.request({
      url: app.globalData.host+'Video/index_free_one',
      method:'POST',
      data:{file_id,token},
      success(res){
        this.getVideoList(that.data.page)
        // let curvideo = that.data.curvideo
        // curvideo = res.data.data
        // that.setData({
        //   curvideo
        // })
      }
    })
  },
  // 点赞
	laudtap(e){
    // console.log(e)
    let curvideo = this.data.videoList[e.currentTarget.dataset.index]
    this.setData({
      curvideo,
    })
    console.log(this.data.curvideo)
	  const that = this
    var v_like = that.data.curvideo.video_like
    var token = wx.getStorageSync('token')
    var file_id = e.currentTarget.dataset.id
	  if(v_like==0){
      // 点赞
      let status = 1
	    wx.request({
	      url: app.globalData.host+'VideoToken/up',
	      method:'POST',
	      data:{status,file_id,token},
	      success(res){
	        if(res.data.code==5001){
	          wx.showToast({
	            title: 'token已失效,请您重新登录',
	            icon:'none'
	          })
	          wx.switchTab({
	            url: '/pages/personal/personal',
	          })
	          return
	        }else if(res.data.code==2001){
	          wx.showToast({
	              title: res.data.msg,
	              icon:'none'
	          })
	          return
	        }
	        wx.showToast({
	          title: res.data.msg,
	          icon:'none'
          })
          that.getVideoList(that.data.page)
          // that.updateLaud(file_id,e.currentTarget.dataset.index)
          let likes = res.data.data
	        that.setData({
            nolaud:false,
            likes
	        })
	      },
	      fail(err){
          wx.showToast({
	          title: res.data.msg,
	          icon:'none'
	        })
	      }
	    })
	  }else if(v_like==1){
      // 取消点赞
      let status = 0
      console.log(file_id)
	    wx.request({
	      url: app.globalData.host+'VideoToken/up',
	      method:'POST',
	      data:{status,file_id,token},
	      success(res){
	        if(res.data.code==5001){
	          wx.showToast({
	            title: 'token已失效,请您重新登录',
	            icon:'none'
	          })
	          wx.switchTab({
	            url: '/pages/personal/personal',
	          })
	          return
	        }
	        wx.showToast({
	          title: res.data.msg,
	          icon:'none'
          })
          // that.updateLaud(file_id,e.currentTarget.dataset.index)
          let likes = res.data.data
	        that.setData({
            nolaud:true,
            likes
	        })
	      },
	      fail(err){
	        console.log(err)
	      }
	    })
	  }else{
      wx.showToast({
        title: '请登录后再点赞',
        icon:'none',
        duration:2000
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getVideoList()
    this.getBanner()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var page = this.data.page || 1;
    page++;
    wx.showLoading({
      title: '正在加载...',
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 500)
    if (this.data.pageNum < 6) {
      wx.showToast({
        title: '没有更多视频了',
        icon: 'none',
      })
      return
    }
    this.getVideoList(page);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})